package com.cuiweiyou.bluemesh.util;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

public class WriteListUtil {
    public static int FLAG_REQUEST_PERMISSION = 102;

    private static String HuaweiString = "应用启动管理 -> 关闭应用开关 -> 打开允许自启动";
    private static String XiaomiString = "授权管理 -> 自启动管理 -> 允许应用自启动";
    private static String OppoString = "权限隐私 -> 自启动管理 -> 允许应用自启动";
    private static String VivoString = "权限管理 -> 自启动 -> 允许应用自启动";
    private static String MeizuString = "权限管理 -> 后台管理 -> 点击应用 -> 允许后台运行";
    private static String SamsungString = "自动运行应用程序 -> 打开应用开关 -> 电池管理 -> 未监视的应用程序 -> 添加应用";
    private static String LetvString = "自启动管理 -> 允许应用自启动";
    private static String SmartisanString = "权限管理 -> 自启动权限管理 -> 点击应用 -> 允许被系统启动";
    private static String NormalString = "从安全设置相关系统应用中查找自启动相关界面进行设置";

    private WriteListUtil() {
    }

    public static String getNote() {
        if (Build.MANUFACTURER.equalsIgnoreCase("xiaomi") || Build.BRAND.toLowerCase().equals("xiaomi")) {
            return XiaomiString;
        } else if (Build.MANUFACTURER.equalsIgnoreCase("huawei") || Build.BRAND.toLowerCase().equals("huawei")) {
            return HuaweiString;
        } else if (Build.MANUFACTURER.equalsIgnoreCase("meizu") || Build.BRAND.toLowerCase().equals("meizu")) {
            return MeizuString;
        } else if (Build.MANUFACTURER.equalsIgnoreCase("vivo") || Build.BRAND.toLowerCase().equals("vivo")) {
            return VivoString;
        } else if (Build.MANUFACTURER.equalsIgnoreCase("oppo") || Build.BRAND.toLowerCase().equals("oppo")) {
            return OppoString;
        } else if (Build.MANUFACTURER.equalsIgnoreCase("360") || Build.BRAND.toLowerCase().equals("360")) {
            return NormalString;
        } else if (Build.MANUFACTURER.equalsIgnoreCase("samsung") || Build.BRAND.toLowerCase().equals("samsung")) {
            return SamsungString;
        } else if (Build.MANUFACTURER.equalsIgnoreCase("letv") || Build.BRAND.toLowerCase().equals("letv")) {
            return LetvString;
        } else if (Build.MANUFACTURER.equalsIgnoreCase("smartisan") || Build.BRAND.toLowerCase().equals("smartisan")) {
            return SmartisanString;
        } else {
            return NormalString;
        }
    }

    public static void startAutoRunSettingActivity(Activity activity) {
        if (Build.MANUFACTURER.equalsIgnoreCase("xiaomi") || Build.BRAND.toLowerCase().equals("xiaomi")) {
            gotoMiuiPermission(activity);
        } else if (Build.MANUFACTURER.equalsIgnoreCase("huawei") || Build.BRAND.toLowerCase().equals("huawei")) {
            gotoHuaweiPermission(activity);
        } else if (Build.MANUFACTURER.equalsIgnoreCase("meizu") || Build.BRAND.toLowerCase().equals("meizu")) {
            gotoMeizuPermission(activity);
        } else if (Build.MANUFACTURER.equalsIgnoreCase("vivo") || Build.BRAND.toLowerCase().equals("vivo")) {
            gotoVivoPermission(activity);
        } else if (Build.MANUFACTURER.equalsIgnoreCase("oppo") || Build.BRAND.toLowerCase().equals("oppo")) {
            gotoOppoPermission(activity);
        } else if (Build.MANUFACTURER.equalsIgnoreCase("360") || Build.BRAND.toLowerCase().equals("360")) {
            goto360Permission(activity);
        } else if (Build.MANUFACTURER.equalsIgnoreCase("samsung") || Build.BRAND.toLowerCase().equals("samsung")) {
            gotoSamsungPermission(activity);
        } else if (Build.MANUFACTURER.equalsIgnoreCase("letv") || Build.BRAND.toLowerCase().equals("letv")) {
            gotoLetvPermission(activity);
        } else if (Build.MANUFACTURER.equalsIgnoreCase("smartisan") || Build.BRAND.toLowerCase().equals("smartisan")) {
            gotoSmartisanPermission(activity);
        } else {
            startAppSettingActivity(activity);
        }
    }

    /**
     * 360的权限管理页面
     */
    private static void goto360Permission(Activity activity) {
        try {
            // com.qiku.android.security/.ui.activity.AppSecurityMainListActivity
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ComponentName comp = new ComponentName("com.qiku.android.security", "com.qiku.android.security.ui.activity.AppSecurityMainListActivity");
            intent.setComponent(comp);
            activity.startActivityForResult(intent, FLAG_REQUEST_PERMISSION);
        } catch (Exception e) {
            startAppSettingActivity(activity);
        }
    }

    /**
     * 三星的权限管理页面
     */
    private static void gotoSamsungPermission(Activity activity) {
        try {
            Intent intent = activity.getPackageManager().getLaunchIntentForPackage("com.samsung.android.sm_cn");
            activity.startActivityForResult(intent, FLAG_REQUEST_PERMISSION);
        } catch (Exception e) {
            gotoSamsungPermission2(activity);
        }
    }

    /**
     * 三星的权限管理页面
     */
    private static void gotoSamsungPermission2(Activity activity) {
        try {
            Intent intent = activity.getPackageManager().getLaunchIntentForPackage("com.samsung.android.sm");
            activity.startActivityForResult(intent, FLAG_REQUEST_PERMISSION);
        } catch (Exception e) {
            startAppSettingActivity(activity);
        }
    }

    /**
     * 锤子的权限管理页面
     */
    private static void gotoSmartisanPermission(Activity activity) {
        try {
            Intent intent = activity.getPackageManager().getLaunchIntentForPackage("com.smartisanos.security");
            activity.startActivityForResult(intent, FLAG_REQUEST_PERMISSION);
        } catch (Exception e) {
            startAppSettingActivity(activity);
        }
    }

    /**
     * 乐视的权限管理页面
     */
    private static void gotoLetvPermission(Activity activity) {
        try {
            Intent i = new Intent();
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ComponentName comp = new ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.AutobootManageActivity");
            i.setComponent(comp);
            activity.startActivityForResult(i, FLAG_REQUEST_PERMISSION);
        } catch (Exception e) {
            startAppSettingActivity(activity);
        }
    }

    /**
     * oppo的权限管理页面
     */
    private static void gotoOppoPermission(Activity activity) {
        try {
            Intent intent = activity.getPackageManager().getLaunchIntentForPackage("com.oppo.safe");
            if (intent != null) {
                activity.startActivityForResult(intent, FLAG_REQUEST_PERMISSION);
            } else {
                startAppSettingActivity(activity);
            }
        } catch (Exception e) {
            e.printStackTrace();
            gotoOppoPermission2(activity);
        }
    }

    /**
     * oppo的权限管理页面
     */
    private static void gotoOppoPermission2(Activity activity) {
        try {
            Intent intent = activity.getPackageManager().getLaunchIntentForPackage("com.coloros.phonemanager");
            activity.startActivityForResult(intent, FLAG_REQUEST_PERMISSION);
        } catch (Exception e1) {
            try {
                Intent intent = activity.getPackageManager().getLaunchIntentForPackage("com.coloros.oppoguardelf");
                activity.startActivityForResult(intent, FLAG_REQUEST_PERMISSION);
            } catch (Exception e3) {
                gotoOppoPermission3(activity);
            }
        }
    }

    /**
     * oppo的权限管理页面
     */
    private static void gotoOppoPermission3(Activity activity) {
        try {
            Intent intent = activity.getPackageManager().getLaunchIntentForPackage("com.coloros.safecenter");
            activity.startActivityForResult(intent, FLAG_REQUEST_PERMISSION);
        } catch (Exception e3) {
            startAppSettingActivity(activity);
        }
    }

    /**
     * vivo的权限管理页面
     */
    private static void gotoVivoPermission(Activity activity) {
        try {
            Intent intent = activity.getPackageManager().getLaunchIntentForPackage("com.iqoo.secure");
            if (intent != null) {
                activity.startActivityForResult(intent, FLAG_REQUEST_PERMISSION);
            } else {
                startAppSettingActivity(activity);
            }
        } catch (Exception e1) {
            e1.printStackTrace();
            startAppSettingActivity(activity);
        }
    }

    /**
     * 跳转到魅族的权限管理系统
     */
    private static void gotoMeizuPermission(Activity activity) {
        try {
            Intent intent = activity.getPackageManager().getLaunchIntentForPackage("com.meizu.safe");
            activity.startActivityForResult(intent, FLAG_REQUEST_PERMISSION);
        } catch (Exception e3) {
            startAppSettingActivity(activity);
        }
    }

    /**
     * 华为的权限管理页面
     */
    private static void gotoHuaweiPermission(Activity activity) {
        try {
            Intent i = new Intent();
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ComponentName comp = new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.bootstart.BootStartActivity");//华为权限管理
            i.setComponent(comp);
            activity.startActivityForResult(i, FLAG_REQUEST_PERMISSION);
        } catch (Exception e) {
            e.printStackTrace();
            gotoHuaweiPermission2(activity);
        }
    }

    /**
     * 华为的权限管理页面
     */
    private static void gotoHuaweiPermission2(Activity activity) {
        try {
            Intent i = new Intent();
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ComponentName comp = new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity");//华为权限管理
            i.setComponent(comp);
            activity.startActivityForResult(i, FLAG_REQUEST_PERMISSION);
        } catch (Exception e) {
            e.printStackTrace();
            startAppSettingActivity(activity);
        }
    }

    /**
     * 跳转到miui的权限管理页面
     */
    private static void gotoMiuiPermission(Activity activity) {
        try {
            Intent i = new Intent("miui.intent.action.APP_PERM_EDITOR");
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ComponentName componentName = new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity");
            i.setComponent(componentName);

            activity.startActivityForResult(i, FLAG_REQUEST_PERMISSION);
        } catch (Exception e) {
            e.printStackTrace();
            startAppSettingActivity(activity);
        }
    }

    private static void startAppSettingActivity(Activity activity) {
        activity.startActivityForResult(getAppDetailSettingIntent(activity), FLAG_REQUEST_PERMISSION);
    }

    /**
     * 获取应用详情页面intent
     *
     * @return
     */
    private static Intent getAppDetailSettingIntent(Context context) {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            //  localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            localIntent.setAction(Intent.ACTION_VIEW);
            localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            localIntent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
        }
        return localIntent;
    }
}
